﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project_KhuyenMaiVang.Models;
using Project_KhuyenMaiVang.DataProvider;
using System.Data.SqlClient;
using System.Data;

namespace Project_KhuyenMaiVang.DAO
{
    public class DoanhNghiepDAO : MyDataProvider
    {
        // Lấy thông tin doanh nghiệp cung cấp tin khuyến mãi có mã doanh nghiệp
        // return doanh nghiệp
        public DoanhNghiepDTO getBussinessInfo(int idBussiness)
        {
            DoanhNghiepDTO result = new DoanhNghiepDTO();
            try
            {
                string strCommand = "SELECT * FROM DOANHNGHIEP WHERE IDAccount = " + idBussiness.ToString() + " and 'True' = (select TinhTrang from Account where IDAccount = " + idBussiness.ToString() + ")";
                _Adapter = new SqlDataAdapter(strCommand, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];
                object ob = GetDataFromDataRow(dt, 0);
                result = (DoanhNghiepDTO)ob;
            }
            catch (Exception ex)
            {
                result = null;
                ex.GetBaseException();
            }

            return result;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            DoanhNghiepDTO doanhNghiep = new DoanhNghiepDTO();
            doanhNghiep.IDAccount = Int32.Parse(dt.Rows[i]["IDAccount"].ToString());
            doanhNghiep.MaLienHe = Int32.Parse(dt.Rows[i]["MaLienHe"].ToString());
            doanhNghiep.TenDoanhNghiep = dt.Rows[i]["TenDoanhNghiep"].ToString();
            doanhNghiep.Web = dt.Rows[i]["Web"].ToString();

            return (object)doanhNghiep;
        }

        public List<DoanhNghiepDTO> getAllDoanhNghiep()
        {
            try
            {
                List<DoanhNghiepDTO> result = new List<DoanhNghiepDTO>();
                string query = "SELECT * FROM DOANHNGHIEP dn where 'True' = (select TinhTrang from Account ac where ac.IDAccount = dn.IDAccount)";
                _Adapter = new SqlDataAdapter(query, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    object ob = GetDataFromDataRow(dt, i);
                    result.Add((DoanhNghiepDTO)ob);
                }
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        public bool updateInfo(int idAccount, string tenDoanhNghiep, string trangWeb)
        {
            bool isOK = true;
            try
            {
                string updateCommand = "UPDATE DoanhNghiep " +
                                        " SET TenDoanhNghiep = N'" + tenDoanhNghiep + "', " +
                                        " Web = '" + trangWeb + "'"
                                        + " where IDAccount = " + idAccount.ToString();

                executeNonQuery(updateCommand);
            }
            catch
            {
                isOK = false;
            }

            return isOK;
        }
    }
}